# coding:utf-8

import time
import datetime
import timeit


# 时间转为时间戳
# 格式化时间字符串
# 转化时间戳和元组到时间字符串
# 时间加减

# 01 import time
# print(type(time.time()), time.time())  # time.time([t])
# print(type(time.localtime()), time.localtime())  # 当地时间 --> struct_time
# asctime = time.asctime(time.localtime())
# print(time.asctime(time.localtime(time.time())))
# print(type(asctime), time.asctime(time.localtime()))
# <class 'float'> 1637657934.6848626
# <class 'time.struct_time'> time.struct_time(tm_year=2021, tm_mon=11, tm_mday=23, tm_hour=16, tm_min=58, tm_sec=54, tm_wday=1, tm_yday=327, tm_isdst=0)
# Tue Nov 23 16:58:54 2021
# <class 'str'> Tue Nov 23 16:58:54 2021

# print(time.mktime(time.localtime()))  # 它的参数是 struct_time 或者完整的 9 元组, 表示 local 的时间，而不是 UTC
# 1637658251.0

# print(type(time.sleep(0.1)), time.sleep(1))
# <class 'NoneType'> None

# print(type(time.gmtime()), time.gmtime())  # 格林威治时间
# print(type(time.gmtime()), time.gmtime(time.time()))
# print(type(time.localtime()), time.localtime())
# <class 'time.struct_time'> time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=2, tm_min=19, tm_sec=6, tm_wday=2, tm_yday=328, tm_isdst=0)
# <class 'time.struct_time'> time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=2, tm_min=19, tm_sec=6, tm_wday=2, tm_yday=328, tm_isdst=0)
# <class 'time.struct_time'> time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=10, tm_min=19, tm_sec=6, tm_wday=2, tm_yday=328, tm_isdst=0)


# strftime 用于对 struct time 对象进行格式化  time--> str
# strptime 用于对字符串对象进行格式化。         str --> time
# str_time = '2011-05-05 16:37:06'
# print(type(time.strptime(str_time, '%Y-%m-%d %X')), time.strptime(str_time, '%Y-%m-%d %X'))
# print(type(time.strftime('%Y-%m-%d %X', time.localtime())), time.strftime('%Y-%m-%d %X', time.localtime()))
# print(type(time.strftime('%x %X', time.localtime())), time.strftime('%x %X', time.localtime()))
# <class 'time.struct_time'> time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)
# <class 'str'> 2021-11-24 10:26:21
# <class 'str'> Wed Nov 24 10:40:48 2021
# <class 'str'> Wed Nov 24 02:40:48 2021

# print(type(time.ctime()), time.ctime())
# print(type(time.asctime(time.localtime())), time.asctime(time.localtime()))
# print(type(time.asctime(time.gmtime())), time.asctime(time.gmtime()))
# <class 'str'> Wed Nov 24 10:40:48 2021
# <class 'str'> Wed Nov 24 10:40:48 2021
# <class 'str'> Wed Nov 24 02:40:48 2021

# time.sleep(1)

# time加减
# t1 = time.time()
# t2 = t1 + 10
# print(time.ctime(t1))
# print(time.ctime(t2))
# Wed Nov 24 10:58:27 2021
# Wed Nov 24 10:58:37 2021

# print(time.localtime(t1))
# print(time.localtime(t2))
# time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=10, tm_min=59, tm_sec=19, tm_wday=2, tm_yday=328, tm_isdst=0)
# time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=10, tm_min=59, tm_sec=29, tm_wday=2, tm_yday=328, tm_isdst=0)


# print(time.gmtime(t1))
# print(time.gmtime(t2))
# time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=3, tm_min=0, tm_sec=6, tm_wday=2, tm_yday=328, tm_isdst=0)
# time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=3, tm_min=0, tm_sec=16, tm_wday=2, tm_yday=328, tm_isdst=0)



# 02 import datetime
# datatime模块重新封装了time模块，提供更多接口，提供的类有：date,time,datetime,timedelta,tzinfo。

# 02-1 datetime.date
# now = datetime.datetime.now()
# utcnow = datetime.datetime.utcnow()
# print(type(now), now)  # 获取当地时间
# print(type(utcnow), utcnow)  # 用于获取 UTC 时间
# <class 'datetime.datetime'> 2021-11-12 14:13:26.943912
# <class 'datetime.datetime'> 2021-11-12 06:13:26.943923

# print()
# print(type(now.year), now.year)
# print(type(now.month), now.month)
# print(type(now.day), now.day)
# <class 'int'> 2021
# <class 'int'> 11
# <class 'int'> 12

# print(type(now.hour), now.hour)
# print(type(now.minute), now.minute)
# print(type(now.second), now.second)
# <class 'int'> 14
# <class 'int'> 21
# <class 'int'> 40

# print(type(now.date()), now.date())
# print(type(now.time()), now.time())
# print(type(now.today()), now.today())
# <class 'datetime.date'> 2021-11-12
# <class 'datetime.time'> 14:22:24.955795
# <class 'datetime.datetime'> 2021-11-12 14:21:40.637447

# print(datetime.datetime.today())
# 2021-11-12 14:16:40.641100

# print(type(datetime.date.today()), datetime.date.today())
# <class 'datetime.date'> 2021-11-24

# format datetime 时间格式化
# strftime 用于对 datetime 对象进行格式化  datetime--> str
# strptime 用于对字符串对象进行格式化。      str --> datetime
# strftime = now.strftime('%Y-%m-%d %H:%M:%S')
# print(type(strftime), strftime)
# <class 'str'> 2021-11-12 14:22:24

# strftime = now.strftime('%Y-%m-%d %H:%M:%S')
# print(type(strftime), strftime)
# <class 'str'> 2021-11-12 14:35:52

# strftime1 = now.strftime('%y-%m-%d %H:%M:%S.%f')
# print(type(strftime1), strftime1)
# <class 'str'> 21-11-12 14:35:52.454998

# strptime = now.strptime(strftime, '%Y-%m-%d %H:%M:%S')
# print(type(strptime), strptime)
# <class 'datetime.datetime'> 2021-11-12 14:26:23

# print(type(datetime.date.fromtimestamp(time.time())), datetime.date.fromtimestamp(time.time()))
# print(type(datetime.datetime.fromtimestamp(time.time())), datetime.datetime.fromtimestamp(time.time()))
# <class 'datetime.date'> 2021-11-24
# <class 'datetime.datetime'> 2021-11-24 11:10:34.233379

# print(type(datetime.date.max), datetime.date.max)
# print(type(datetime.date.min), datetime.date.min)
# <class 'datetime.date'> 9999-12-31
# <class 'datetime.date'> 0001-01-01

# print(type(datetime.date.resolution), datetime.date.resolution)
# <class 'datetime.timedelta'> 1 day, 0:00:00

# d1 = datetime.date(2021, 11, 24)
# print(type(d1), d1)
# print(type(d1.replace(2012, 1, 26)), d1.replace(2012, 1, 26))
# print(type(d1.timetuple()), d1.timetuple())
# print(type(d1.weekday()), d1.weekday())
# print(type(d1.isoweekday()), d1.isoweekday())
# print(type(d1.isocalendar()), d1.isocalendar())
# print(type(d1.isoformat()), d1.isoformat())
# print(str(d1))
# print(d1.strftime('%Y-%m-%d'))
# print(d1.strftime('%x'))
# print(d1.strftime('%c'))
# <class 'datetime.date'> 2021-11-24
# <class 'datetime.date'> 2012-01-26
# <class 'time.struct_time'> time.struct_time(tm_year=2021, tm_mon=11, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=328, tm_isdst=-1)
# <class 'int'> 2
# <class 'int'> 3
# <class 'tuple'> (2021, 47, 3)
# <class 'str'> 2021-11-24
# 2021-11-24
# 2021-11-24
# 11/24/21
# Wed Nov 24 00:00:00 2021


# 02-2 datetime.time
# print(type(datetime.time.max), datetime.time.max)
# print(type(datetime.time.min), datetime.time.min)
# print(type(datetime.time.resolution), datetime.time.resolution)  # 时间的最小单位，这里是1微秒
# <class 'datetime.time'> 23:59:59.999999
# <class 'datetime.time'> 00:00:00
# <class 'datetime.timedelta'> 0:00:00.000001

t1 = datetime.time(11, 30, 23)
# print(type(t1.hour), t1.hour)
# print(type(t1.minute), t1.minute)
# print(type(t1.second), t1.second)
# print(type(t1.tzinfo), t1.tzinfo)
# print(type(t1.tzname()), t1.tzname())


# <class 'int'> 11
# <class 'int'> 30
# <class 'int'> 23
# <class 'NoneType'> None
# <class 'NoneType'> None

# 时间戳


